package cn.t.converter.order;


import cn.t.core.mode.YesNoEnum;
import cn.t.dto.device.CargowayDTO;
import cn.t.dto.order.CreateOrderRequest;
import cn.t.dto.order.DislodgeOrderDTO;
import cn.t.dto.order.OrderDTO;
import cn.t.dto.order.OrderDetailDTO;
import cn.t.dto.order.excelresp.DislodgeExcelResp;
import cn.t.dto.order.excelresp.ErrorExcelResp;
import cn.t.dto.sku.SkuDTO;
import cn.t.enums.OverPickupTypeEnum;
import cn.t.enums.PushStatusEnum;
import cn.t.enums.TimeLimitTypeEnum;
import cn.t.enums.device.DeviceTypeEnum;
import cn.t.enums.order.ErrorTypeEnum;
import cn.t.enums.order.MsvmOrderStatusEnum;
import cn.t.enums.order.OrderTransferStatusEnum;
import cn.t.enums.user.ScenarioCodeEnum;
import cn.t.model.device.DeviceDO;
import cn.t.model.order.ExceptionLogDO;
import cn.t.model.order.OrderDO;
import cn.t.model.order.OrderDetailDO;
import cn.t.model.order.OverPickupRecordDO;
import cn.t.model.user.UserQueryDO;
import cn.t.utils.DateUtil;
import cn.t.utils.MsvmUtil;
import cn.t.vo.order.DislodgeOrderDetailVO;
import cn.t.vo.order.ToNormalLineVO;
import cn.t.vo.order.UpdateOrderGoodsVO;
import cn.t.vo.user.ParticipantVO;
import cn.t.vo.user.ParticipantWithUserVO;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author l
 */
public class OrderModelConverter {
    public static OrderDTO convertVoToOrderDto(CreateOrderRequest request, DeviceDO deviceDTO, ParticipantWithUserVO userDO) {
        OrderDTO orderBean = new OrderDTO();
        orderBean.setOrderSn(MsvmUtil.getBdOrderSn(request.getDeviceId()));
        orderBean.setUserId(userDO.getUserId());
            orderBean.setParticipantId(userDO.getParticipantId());

        orderBean.setGmtOpen( DateUtil.closeDate(request.getGmtCreated(), (long) -1));
        orderBean.setGmtClose(DateUtil.dateByStr(request.getGmtCreated()));
        orderBean.setGmtCreated(DateUtil.dateByStr(request.getGmtCreated()));
        orderBean.setOrderStatus(MsvmOrderStatusEnum.NORMAL.getCode());
        orderBean.setOrderTransferStatus(OrderTransferStatusEnum.NOTRANSFER.getCode());
        orderBean.setCostCenterId(ObjectUtils.isEmpty(userDO.getCostCenterId())?null:userDO.getCostCenterId().toString());
        orderBean.setDeviceId(deviceDTO.getId());
        orderBean.setOrderStatus(MsvmOrderStatusEnum.NORMAL.getCode());
        orderBean.setFirmId(deviceDTO.getFirmId());
        orderBean.setUserName(userDO.getIndividualName());
        orderBean.setCardCode(request.getCardCode().trim());
        orderBean.setCostCenterName(userDO.getCostCenterName());
        orderBean.setDeviceName(deviceDTO.getDeviceName());
        return orderBean;
    }
    public static OrderDetailDTO convertVoToOrderDetail(SkuDTO goodsDO, UpdateOrderGoodsVO orderGoods, OrderDO orderDO) {
        OrderDetailDTO orderGoodsBean = new OrderDetailDTO();
        orderGoodsBean.setBeforeWeight(0.0D);
        orderGoodsBean.setAfterWeight(0.0D);
        orderGoodsBean.setOrderTransferStatus(OrderTransferStatusEnum.NOTRANSFER.getCode());
        orderGoodsBean.setErrorRate(0);
        orderGoodsBean.setIsDeleted(YesNoEnum.FALSE);
        orderGoodsBean.setId(orderGoods.getSid());
        orderGoodsBean.setQty(orderGoods.getQty());
        orderGoodsBean.setCategoryId(goodsDO.getCategoryId());
        orderGoodsBean.setCategoryName(goodsDO.getCategoryName());
        orderGoodsBean.setSkuName(goodsDO.getSkuName());
        orderGoodsBean.setSkuImg(goodsDO.getSkuImg());
        orderGoodsBean.setStdWeight(goodsDO.getWeight().doubleValue());
        orderGoodsBean.setSkuCode(goodsDO.getSkuCode());
        orderGoodsBean.setCargowayNo(orderGoods.getCargowayNo());
        return orderGoodsBean;
    }
    public static OrderDetailDO convertToOrderGoods(ExceptionLogDO req, ToNormalLineVO toNormalLineVO, CargowayDTO cargowayDO) {
        OrderDetailDO orderGoodsBean = new OrderDetailDO();
        orderGoodsBean.setCargowayId(cargowayDO.getId());

        orderGoodsBean.setSkuImg(req.getSkuImg());
        orderGoodsBean.setBeforeWeight(0.0D);
        orderGoodsBean.setAfterWeight(0.0D);
        orderGoodsBean.setErrorRate(0);
        orderGoodsBean.setIsDeleted(YesNoEnum.FALSE);
        orderGoodsBean.setQty(toNormalLineVO.getNum());
        if (req.getCategoryId()!=null) {
            orderGoodsBean.setCategoryId(Math.toIntExact(req.getCategoryId()));
        }
        orderGoodsBean.setOrderSn(req.getOrderSn().trim());
        orderGoodsBean.setCategoryName(req.getCategoryName());
        orderGoodsBean.setSkuName(req.getSkuName());
        orderGoodsBean.setStdWeight(req.getStdWeight());
        orderGoodsBean.setSkuCode(req.getSkuCode());
        orderGoodsBean.setCargowayNo(cargowayDO.getCargowayNo());
        orderGoodsBean.setOrderTransferStatus(OrderTransferStatusEnum.TRANSFERRED.getCode());
        return orderGoodsBean;
    }
    public static DislodgeOrderDTO convertBeanToDTO(OrderDO orderBean,
                                                    DeviceDO deviceBean) {
        DislodgeOrderDTO resp = new DislodgeOrderDTO();
        if (!ObjectUtils.isEmpty(orderBean)) {
            resp.setOrderSn(orderBean.getOrderSn());
            resp.setWorkOrderSn(orderBean.getWorkOrderSn());
            resp.setDeviceId(orderBean.getDeviceId());
            resp.setWorkOrderSn(orderBean.getWorkOrderSn());
            resp.setGmtCreatOrder(DateUtil.setTime(orderBean.getGmtCreated()));
            resp.setGmtOpen(DateUtil.setTime(orderBean.getGmtOpen()));
            resp.setGmtClose(DateUtil.setTime(orderBean.getGmtClose()));
            resp.setUserName(orderBean.getUserName());
            resp.setCardCode(orderBean.getCardCode());
            resp.setGmtCreated(DateUtil.setTime(orderBean.getGmtCreated()));
            resp.setCostCenterName(orderBean.getCostCenterName());
            resp.setCostCenterId(orderBean.getCostCenterId());
        }
        if (!ObjectUtils.isEmpty(deviceBean)) {
            resp.setImei(deviceBean.getImei());
            resp.setDeviceType(DeviceTypeEnum.getName(deviceBean.getDeviceType()));
            resp.setDeviceName(deviceBean.getDeviceName());
        }

        return resp;
    }

    public static DislodgeOrderDTO converterDislodgeMain(OrderDO orderDO, UserQueryDO userSessionDTO, DeviceDO deviceDTO) {
        DislodgeOrderDTO orderDTO = new DislodgeOrderDTO();
        orderDTO.setOrderSn(orderDO.getOrderSn());
        orderDTO.setGmtCreated(DateUtil.setTime(orderDO.getGmtCreated()));
        orderDTO.setGmtCreatOrder(DateUtil.setTime(orderDO.getGmtCreated()));
        orderDTO.setWorkOrderSn(orderDO.getWorkOrderSn());
        orderDTO.setGmtOpen(DateUtil.setTime(orderDO.getGmtOpen()));
        orderDTO.setGmtClose(DateUtil.setTime(orderDO.getGmtClose()));
        orderDTO.setCostCenterName(orderDO.getCostCenterName());
        if (deviceDTO != null) {
            orderDTO.setDeviceName(deviceDTO.getDeviceName());
            orderDTO.setDeviceType(DeviceTypeEnum.getName(deviceDTO.getDeviceType()));
            orderDTO.setImei(deviceDTO.getImei());
            orderDTO.setDeviceId(deviceDTO.getId());
        }
        orderDTO.setUserName(orderDO.getUserName());
        orderDTO.setCardCode(orderDO.getCardCode());
        orderDTO.setOrderTransferStatus(orderDO.getOrderTransferStatus());
        orderDTO.setOrderTransferStatusName(
                OrderTransferStatusEnum.getDescByKey(orderDO.getOrderTransferStatus()));
        orderDTO.setPushStatus(PushStatusEnum.getDescByKey(orderDO.getPushStatus()));

        return orderDTO;
    }
    public static int getTotalPages(List<String> orderSnList, int pageSize) {
        return (int) Math.ceil((double) orderSnList.size() / pageSize);
    }
    public static  DislodgeOrderDetailVO convertDislodgeErrVo(ExceptionLogDO orderDetailDO) {
        DislodgeOrderDetailVO orderDetail = new DislodgeOrderDetailVO();
        Double beforeWeight = orderDetailDO.getBeforeWeight();
        Double afterWeight = orderDetailDO.getAfterWeight();
        orderDetail.setBeforeWeight(beforeWeight);
        orderDetail.setAfterWeight(afterWeight);
        orderDetail.setCategoryName(orderDetailDO.getCategoryName());
        orderDetail.setSkuImg(orderDetailDO.getSkuImg());
        orderDetail.setSkuName(orderDetailDO.getSkuName());
        orderDetail.setStdWeight(orderDetailDO.getStdWeight());
        orderDetail.setSkuCode(orderDetailDO.getSkuCode());
        orderDetail.setCargowayNo(orderDetailDO.getCargowayNo());
        //计算 差
        if (beforeWeight != null && afterWeight != null) {
            orderDetail.setWeightClash(beforeWeight - afterWeight);
        }
        orderDetail.setExceptionTypeName(ErrorTypeEnum.getDescByKey(orderDetailDO.getExceptionType()));
        orderDetail.setOrderTransferStatusName(
                OrderTransferStatusEnum.getDescByKey(orderDetailDO.getOrderTransferStatus()));
        orderDetail.setSid(orderDetailDO.getId());
        return orderDetail;
    }
    public static  DislodgeOrderDetailVO convertDislodgeDetail(OrderDetailDO orderDetailDO) {
        DislodgeOrderDetailVO orderDetail = new DislodgeOrderDetailVO();
        Double beforeWeight = orderDetailDO.getBeforeWeight();
        Double afterWeight = orderDetailDO.getAfterWeight();
        orderDetail.setBeforeWeight(beforeWeight);
        orderDetail.setAfterWeight(afterWeight);
        orderDetail.setCategoryName(orderDetailDO.getCategoryName());
        orderDetail.setSkuImg(orderDetailDO.getSkuImg());
        orderDetail.setSkuName(orderDetailDO.getSkuName());
        orderDetail.setStdWeight(orderDetailDO.getStdWeight());
        orderDetail.setSkuCode(orderDetailDO.getSkuCode());
        orderDetail.setCargowayNo(orderDetailDO.getCargowayNo());
        //计算 差
        if (beforeWeight != null && afterWeight != null) {
            orderDetail.setWeightClash(beforeWeight - afterWeight);
            orderDetail.setQty(orderDetailDO.getQty());
        }
        orderDetail.setExceptionTypeName(ErrorTypeEnum.ERROR2.getName());
        orderDetail.setSid(orderDetailDO.getId());
        return orderDetail;
    }
    public static DislodgeExcelResp convertExcelDetail(OrderDetailDO orderDetailDO, DislodgeOrderDTO orderDO) {
        DislodgeExcelResp orderExcelResp = new DislodgeExcelResp();
        if (!ObjectUtils.isEmpty(orderDO)){
            orderExcelResp.setWorkOrderSn(orderDO.getWorkOrderSn());
            orderExcelResp.setGmtCreatOrder(orderDO.getGmtCreated());
            orderExcelResp.setDeviceName(orderDO.getDeviceName());
            orderExcelResp.setDeviceId(orderDO.getDeviceId());
            orderExcelResp.setUserName(orderDO.getUserName());
            orderExcelResp.setCardCode(orderDO.getCardCode());
            orderExcelResp.setCostCenterName(orderDO.getCostCenterName());
            orderExcelResp.setFirmName(orderDO.getFirmName());
            orderExcelResp.setFirmId(orderDO.getFirmId());
            orderExcelResp.setCustomerCode(orderDO.getFirmCode());
        }
        orderExcelResp.setOrderSn(orderDetailDO.getOrderSn());
        orderExcelResp.setSkuName(orderDetailDO.getSkuName());
        orderExcelResp.setSkuCode(orderDetailDO.getSkuCode());
        orderExcelResp.setGoodsWeight(BigDecimal.valueOf(orderDetailDO.getStdWeight()));
        orderExcelResp.setCargowayNo(orderDetailDO.getCargowayNo());
        orderExcelResp.setQty(orderDetailDO.getQty());
        return orderExcelResp;
    }
    public static ErrorExcelResp convertDetailToExcel(OrderDetailDO orderDetailDO, DislodgeOrderDTO orderBean){
        ErrorExcelResp resp = new ErrorExcelResp();
        setOrderInfo(orderBean, resp);
        resp.setSkuName(orderDetailDO.getSkuName());
            resp.setSkuCode(orderDetailDO.getSkuCode());
            resp.setSkuWeight(orderDetailDO.getStdWeight());
            resp.setCargowayNo(orderDetailDO.getCargowayNo());
            resp.setQty(orderDetailDO.getQty());
            resp.setBeforeWeight(orderDetailDO.getBeforeWeight());
            resp.setAfterWeight(orderDetailDO.getAfterWeight());
            resp.setErrorContent("正常");

        return resp;
    }
    public static ErrorExcelResp convertErrBeanToExcel(ExceptionLogDO orderDetailDO, DislodgeOrderDTO orderBean){
        ErrorExcelResp resp = new ErrorExcelResp();
        setOrderInfo(orderBean, resp);
        resp.setSkuName(orderDetailDO.getSkuName());
        resp.setSkuCode(orderDetailDO.getSkuCode());
        resp.setSkuWeight(orderDetailDO.getStdWeight());
        resp.setCargowayNo(orderDetailDO.getCargowayNo());
        resp.setBeforeWeight(orderDetailDO.getBeforeWeight());
        resp.setAfterWeight(orderDetailDO.getAfterWeight());
        resp.setErrorContent( OrderTransferStatusEnum.getDescByKey(orderDetailDO.getOrderTransferStatus()));

        return resp;
    }

    private static void setOrderInfo(DislodgeOrderDTO orderBean, ErrorExcelResp resp) {
        resp.setOrderSn(orderBean.getOrderSn());
        resp.setGmtCreatOrder(orderBean.getGmtCreated());
        resp.setDeviceId(orderBean.getDeviceId());
        resp.setDeviceName(orderBean.getDeviceName());
        resp.setUserName(orderBean.getUserName());
        resp.setCardCode(orderBean.getCardCode());
        resp.setCostCenterName(orderBean.getCostCenterName());
        resp.setWorkOrderSn(orderBean.getWorkOrderSn());
        resp.setOrderTransferStatus(OrderTransferStatusEnum.getDescByKey(orderBean.getOrderTransferStatus()));
    }

    public static OverPickupRecordDO createOverPickupOrder(
            Integer pickupNum //领取数量
            , Integer overNum //超领数量
            , String workOrderSn //工单号
            , TimeLimitTypeEnum timeLimitTypeEnum
            , Integer  quantityLimit,
            OverPickupTypeEnum type, OrderDetailDO orderDetailDO) {
        OverPickupRecordDO overPickupOrderBean = new OverPickupRecordDO();
        overPickupOrderBean.setWorkOrderSn(workOrderSn);
        overPickupOrderBean.setTimeLimitType(timeLimitTypeEnum.getCode());
        overPickupOrderBean.setQuantityLimit(quantityLimit);
        overPickupOrderBean.setSkuName(orderDetailDO.getSkuName());

        overPickupOrderBean.setPickupQty(pickupNum);
        overPickupOrderBean.setOverQty(overNum);
        overPickupOrderBean.setOrderSn(orderDetailDO.getOrderSn());
        overPickupOrderBean.setSkuCode(orderDetailDO.getSkuCode());
        overPickupOrderBean.setType(type.getCode());
        return overPickupOrderBean;
    }
    public static boolean isOverPickup(Integer qty, Integer actQty, Integer planQty) {
        if (actQty == null ){
            actQty = 0;
        }
        //历史领用数量+本次领用数量>计划
        return (actQty + qty) > planQty;
    }
    public static int getOverNum(Integer qty, Integer actQty, Integer planQty) {
        int overNum = 0;
        //判断之前是否超领1.
        if ((actQty + qty) > planQty) {
            //超领数量
            overNum = Math.toIntExact(actQty + qty - planQty);
        }
        if (actQty >= planQty) {
            overNum = qty;
        }
        return overNum;
    }
   public static ParticipantVO getParticipantVO(List<ParticipantVO> participantList,
                                                ScenarioCodeEnum scenarioCodeEnum){
     return participantList.stream().filter(participantDO -> participantDO.getScenarioId().equals(scenarioCodeEnum.getCode())).findFirst().orElse(null);
    }
}
